<!--
 * 登录页 - 科幻风格
 *
 * @Author: ShanZhu
 * @Date: 2023-12-03
-->
<template>
  <div
      class="scifi-login-container"
      v-loading="loading"
      element-loading-text="系统验证中..."
      element-loading-spinner="el-icon-loading"
      element-loading-background="rgba(0, 0, 0, 0.8)"
  >
    <!-- 动态背景元素 -->
    <div class="scifi-bg-elements">
      <div class="floating-orb orb-1"></div>
      <div class="floating-orb orb-2"></div>
      <div class="floating-orb orb-3"></div>
      <div class="grid-lines"></div>
    </div>

    <div class="scifi-login-box" v-if="!registerShow">
      <!-- 头部区域 -->
      <div class="scifi-header">
        <div class="logo-container">
          <div class="hologram-logo">
            <div class="logo-core"></div>
            <div class="logo-ring"></div>
          </div>
        </div>
        <h1 class="scifi-title">慧停车</h1>
        <p class="scifi-subtitle">智能停车解决方案</p>
      </div>

      <!-- 登录表单 -->
      <el-form
          class="scifi-login-form"
          :model="ruleForm"
          :rules="rules"
          ref="rules"
          label-width="0"
      >
        <div class="input-group">
          <div class="input-icon">
            <i class="iconfont icon-user"></i>
          </div>
          <el-form-item prop="username">
            <el-input
                v-model="ruleForm.username"
                placeholder="请输入用户名"
                class="scifi-input"
            >
            </el-input>
          </el-form-item>
        </div>

        <div class="input-group">
          <div class="input-icon">
            <i class="iconfont icon-password"></i>
          </div>
          <el-form-item prop="password">
            <el-input
                type="password"
                v-model="ruleForm.password"
                placeholder="请输入密码"
                class="scifi-input"
                show-password
            >
            </el-input>
          </el-form-item>
        </div>

        <div class="button-group">
          <el-button
              type="primary"
              @click="login"
              class="scifi-btn login-btn"
          >
                        <span class="btn-content">
                            <i class="iconfont icon-login-button"></i>
                            <span>系统登录</span>
                        </span>
            <div class="btn-glow"></div>
          </el-button>

          <el-button
              type="success"
              @click="toRegister"
              class="scifi-btn register-btn"
          >
                        <span class="btn-content">
                            <i class="iconfont icon-register-button"></i>
                            <span>用户注册</span>
                        </span>
            <div class="btn-glow"></div>
          </el-button>
        </div>
      </el-form>
    </div>

    <!-- 注册页面 -->
    <div class="scifi-register-box" v-if="registerShow">
      <div class="register-header">
        <h2 class="register-title">用户注册</h2>
        <p class="register-subtitle">创建新账户</p>
      </div>

      <el-form
          :model="registerUser"
          status-icon
          :rules="registerRules"
          ref="registerRules"
          label-width="120px"
          class="scifi-register-form"
      >
        <div class="form-grid">
          <el-form-item label="用户名" prop="username">
            <el-input
                v-model="registerUser.username"
                placeholder="请输入用户名"
                class="scifi-input"
            ></el-input>
          </el-form-item>

          <el-form-item label="密码" prop="password">
            <el-input
                v-model="registerUser.password"
                placeholder="请输入密码"
                show-password
                class="scifi-input"
            ></el-input>
          </el-form-item>

          <el-form-item label="确认密码" prop="check" class="is-required">
            <el-input
                v-model="registerUser.check"
                placeholder="确认密码"
                show-password
                class="scifi-input"
            ></el-input>
          </el-form-item>

          <el-form-item label="姓名" prop="nike">
            <el-input
                v-model="registerUser.nike"
                placeholder="请输入姓名"
                class="scifi-input"
            ></el-input>
          </el-form-item>

          <el-form-item label="性别" prop="sex" required>
            <el-radio-group v-model="registerUser.sex">
              <el-radio label="男" class="scifi-radio">男</el-radio>
              <el-radio label="女" class="scifi-radio">女</el-radio>
            </el-radio-group>
          </el-form-item>

          <el-form-item label="年龄" prop="age" class="is-required">
            <el-input
                v-model.number="registerUser.age"
                class="scifi-input"
            ></el-input>
          </el-form-item>

          <el-form-item label="车牌号" prop="card" class="is-required">
            <el-input
                v-model="registerUser.card"
                placeholder="请输入车牌号"
                class="scifi-input"
            ></el-input>
          </el-form-item>

          <el-form-item label="联系电话" prop="phone" required>
            <el-input
                v-model="registerUser.phone"
                placeholder="请输入联系电话"
                class="scifi-input"
            ></el-input>
          </el-form-item>
        </div>

        <div class="register-actions">
          <el-button
              class="scifi-btn confirm-register-btn"
              type="success"
              @click="submitForm('registerRules')"
          >
                        <span class="btn-content">
                            <i class="iconfont icon-register-button"></i>
                            <span>确认注册</span>
                        </span>
            <div class="btn-glow"></div>
          </el-button>
          <el-button
              type="primary"
              class="scifi-btn back-login-btn"
              @click="toLogin"
          >
                        <span class="btn-content">
                            <i class="iconfont icon-back-button"></i>
                            <span>返回登录</span>
                        </span>
            <div class="btn-glow"></div>
          </el-button>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
import axios from "axios";
export default {
  data() {
    var checkUsername = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("用户名不能为空"));
      }
      setTimeout(() => {
        axios
            .get("/api/user/checkUsername?username=" + value)
            .then((res) => {
              if (!res.data.data) {
                callback(new Error("用户名已存在"));
              } else {
                callback();
              }
            });
      }, 10);
    };
    var checkAge = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("年龄不能为空"));
      }
      setTimeout(() => {
        if (!Number.isInteger(value)) {
          callback(new Error("请输入数字值"));
        } else {
          if (value < 18) {
            callback(new Error("必须年满18岁"));
          } else {
            callback();
          }
        }
      }, 1000);
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.registerUser.password) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      registerShow: false,
      loading: false,
      ruleForm: {
        username: "",
        password: "",
      },
      rules: {
        username: [
          {
            required: true,
            message: "请输入用户名",
            trigger: "blur",
          },
          {
            min: 3,
            max: 10,
            message: "长度在 3 到 10 个字符",
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 6,
            max: 15,
            message: "长度在 6 到 15 个字符",
            trigger: "blur",
          },
        ],
      },
      registerUser: {
        username: "",
        password: "",
        check: "",
        sex: "男",
        phone: "",
        age: "",
        card: "",
        nike: "",
      },
      registerRules: {
        username: [{ validator: checkUsername, trigger: "blur" }],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 6,
            max: 15,
            message: "长度在 6 到 15 个字符",
            trigger: "blur",
          },
        ],
        check: [{ validator: validatePass2, trigger: "blur" }],
        age: [{ validator: checkAge, trigger: "blur" }],
        phone: [
          {
            required: true,
            message: "请输入联系电话",
            trigger: "blur",
          },
        ],
        card: [
          {
            required: true,
            message: "请输入车牌号",
            trigger: "blur",
          },
        ],
        nike: [
          {
            required: true,
            message: "请输入姓名",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    login() {
      this.loading = true;
      axios
          .post("/api/user/login", this.ruleForm)
          .then((res) => {
            this.loading = false;
            if (res.data.data.state) {
              this.$message.success("登陆成功！");
              //  token只应在当前网站打开期间生效，所以将token保存在sessionStorage中
              window.sessionStorage.setItem(
                  "token",
                  res.data.data.uid
              );
              if (res.data.data.role == 0) {
                this.$router.push("/mane");
              } else {
                this.$router.push("/conmen");
              }
            } else {
              this.$message.error(
                  "登录失败," + res.data.data.msg + "!"
              );
            }
          })
          .catch((error) => {
            console.log(error.message);
            this.loading = false;
            this.$message.error("服务器异常");
          });
    },
    toRegister() {
      this.registerShow = true;
    },
    toLogin() {
      this.registerShow = false;
      this.$nextTick(() => {
        this.$refs.rules.resetFields();
      });
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.loading = true;
          axios
              .post("/api/user/register", this.registerUser)
              .then((res) => {
                this.loading = false;
                if (res.data.data) {
                  this.$message.success("注册成功！");
                  this.registerShow = false;
                } else {
                  this.$message.error("登录失败,请重新尝试！");
                }
              });
        } else {
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>

<style lang="less" scoped>
@import 'styles/Login.less';
</style>